<?php
/**
 * Created by PhpStorm.
 * User: t.young
 * Date: 2018/5/21
 * Time: 13:34
 */

namespace backend\service;
use backend\component\BackendBaseService;
use backend\component\ResultEasyUIFormat;
use common\models\info\Article;
use backend\component\ResultFormat;

class ArticleService extends BackendBaseService
{

    /**
     * 获取分类列表信息
     * @param $post
     * @return ResultEasyUIFormat
     */
    public static function articleList($get)
    {
        $model = new Article();
        $table = $model->dataListTable($get);
        return ResultEasyUIFormat::tableFormat($table)->asJson();
    }
    public static function auditArticle($id,$post){
        $model = Article::modelById($id);
        $model->tag_str = $post['tag_str']!='false'?$post['tag_str']:'';
        $model->status = $post['status'];
        $model->release_at = $post["status"] == Article::STATUS_PASS ? $model->release_at : 0;
        if(!$model->save()){
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("审核成功");
    }
    public static function deleteArticle($id){
        $model = Article::findOne($id);
        if (!$model || $model["id"] != $id) {
            return ResultFormat::error("这条数据不存在!");
        }
        try{
            if (!$model->delete()) {
                return ResultFormat::error($model->getErrorsToString());
            }
            return ResultFormat::success("删除成功");
        }catch (\Exception $e) {
            return ResultFormat::error($e->getMessage());
        }
    }
}
